import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { user_info } from '../login/login';



/**
 * Generated class for the PersonalMessagePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
	selector: 'page-personal-message',
	templateUrl: 'personal-message.html',
})
export class PersonalMessagePage {

	phone: any;
	nickname: any;
	member_id: any;

	public data = { img: null };
	//用户信息
	userInfo: user_info = new user_info();

	constructor(
		public navCtrl: NavController,
		public viewCtrl: ViewController,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public user: UserInfoProvider) { }

  ionViewDidLoad() {
	console.log('ionViewDidLoad PersonalMessagePage');
	this.userInfo=this.user.getuserInfo();
	this.member_id=this.userInfo.member_id;
	this.nickname=this.userInfo.nickname;
//	this.phone=this.userInfo.phone;
	this.phone=this.phone2();
	this.getImg();
	//  this.storage.getStorage("login_info").then(res => {
	// 	if(res != undefined) {
	// 		this.phone=res['phone']
	// 		this.nickname=res['nickname']
	// 		this.member_id=res['member_id']
	// 		this.getImg();
	// 	}
	// });
  }
    phone2() {
		//phone1截取手机号前3位
		var phone1 = this.user.userInfo.phone.substring(3, 0)
		//var phone1=this.phone_name.substring(0,3)//截取时包括前不包括后面，下标从0开始
		console.log(phone1)
		//phone2隐藏手机号
		var phone2 = "****"
		//phone3截取到手机号7-11位，下标从0开始
		var phone3 = this.user.userInfo.phone.substring(7)
		console.log(phone3)
		var phone4 = phone1 + phone2 + phone3
		return phone4;

	}
	skipToNickNameEdit() {
		this.navCtrl.push("NicknameEditPage");
	}

	skipToImgEdit() {
		this.navCtrl.push("ImgEditPage");
	}


	//   获取图片方法
	getImg() {
		this.http.postJson('', 'system/middlefile/index', {
			"joinwith": ["photos"],
			"where": ["and", ["=", "product_id", this.member_id], ["=", "file_type_id", "1"], ["=", "tables", "1"]],
			"limit": 0
		}, '').then(res => {
			console.log(res)
			if (res['status'] == 200) {
				this.getFile_one(res['data']);
			}
		});

	}




	//    把数组数据遍历,重新组成新的数据Push到数组里
	getFile_one(data) {
		console.log(data)
		//图片参数为空,赋给用户默认的头像
		if (data.length === 0) {
			this.data.img = "./assets/imgs/user.png";
			//this.data.img="../../assets/imgs/user.png";
		}
		else if (data[0].photos) {
			console.log(data[0].photos)
			this.data.img = this.http.APP_IMG + data[0].photos.url;
		}
		console.log(this.data)
	}
}
